package com.example.mydownloader.mymodule.http

import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
import org.jetbrains.annotations.NotNull

/**
 * @Author: Wallace
 * @Description: 文件描述
 * @Date: 2022/1/3 13:06
 * @Modified: By yyyy-MM-dd
 */
object MyCookie : CookieJar {
    private val cookieStore: MutableMap<HttpUrl, List<Cookie>> = HashMap()

    fun clearCookies() {
        cookieStore.clear()
    }

    override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
        cookieStore[url] = cookies
    }

    @NotNull
    override fun loadForRequest(url: HttpUrl): List<Cookie> {
        val cookies = cookieStore[url]
        return cookies ?: ArrayList()
    }
}